﻿using System;
using System.Data;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using Pantry.Business;
using Pantry.Commons;

namespace Pantry.Web.Users.Controls
{
    public delegate void AlertToParent(string a);
    public partial class ServicePremise : System.Web.UI.UserControl
    {
        public event AlertToParent AlertToParents;
        private ServicePremiseBus _servicePremiseBus;
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadByServiceId();
            LoadActivePremiseNotBelongService();
            UpdatePanel1.UpdateMode = UpdatePanelUpdateMode.Conditional;
            UpdatePanel1.Update();
            if (AlertToParents != null)
            {
                AlertToParents("a");
            }
        }
        protected void LoadByServiceId()
        {
            try
            {
                _servicePremiseBus = new ServicePremiseBus();
                if (Request.QueryString["id"] != null)
                {
                    int serviceId = int.Parse(Request.QueryString["id"]);
                    DataTable listPremise = _servicePremiseBus.SelectServicePremiseByServiceId(serviceId);
                    RepeaterListPremises.DataSource = listPremise;
                    RepeaterListPremises.DataBind();
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void LoadActivePremiseNotBelongService()
        {
            try
            {
                _servicePremiseBus = new ServicePremiseBus();
                DataTable dataTable = _servicePremiseBus.SelectServicePremiseNotBelongService();
                RepeaterActivePremiseNotBelongService.DataSource = dataTable;
                RepeaterActivePremiseNotBelongService.DataBind();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
        protected void ButtonSelectClick(object sender, EventArgs e)
        {
            try
            {
                if (HttpHelper.GetFormValue("radioPremiseId") == null)
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, GetType(), "Infomation", "alert('Please select one Premise !')", true);
                }
                else
                {
                    _servicePremiseBus = new ServicePremiseBus();
                    string serviceId = Request.QueryString["id"];
                    int premiseId = int.Parse(HttpHelper.GetFormValue("radioPremiseId"), CultureInfo.CurrentCulture);
                    string projectCode = TextBoxProjectCode.Text;
                    bool result = _servicePremiseBus.UpdatePremiseAddToService(projectCode, serviceId, premiseId);
                    if (result)
                    {

                        LoadByServiceId();
                        LoadActivePremiseNotBelongService();
                        ScriptManager.RegisterStartupScript(UpdatePanel1, GetType(), "Notice", "ClosePremisePopup()", true);
                        ScriptManager.RegisterStartupScript(UpdatePanel1, GetType(), "MyKey",
                                                                            "showmessage('Associate new Premise successfully !','SUCCESS','','false');",
                                                                            true);
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(UpdatePanel1, GetType(), "MyKey",
                                                                            "showmessage('Associate new Premise failed ! !','ERROR','','false');",
                                                                            true);
                    }
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
        protected void RepeaterListPremises_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            var hId = (HiddenField)RepeaterListPremises.Items[e.Item.ItemIndex].FindControl("HiddenFieldPremiseId");
            if (e.CommandName == "update")
            {
                int id = int.Parse(hId.Value);
                _servicePremiseBus = new ServicePremiseBus();
                bool result = _servicePremiseBus.UpdatePremiseAddToService(null, null, id);
                LoadByServiceId();
                LoadActivePremiseNotBelongService();
                if (result)
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel2, GetType(), "MyKey",
                                                                            "showmessage('Remove Premise successfully !','SUCCESS','','false');",
                                                                            true);
                }
            }
        }
    }
}